<?php
/*
 * MySQLi Adapter
 */
 
class MySqliAdapter {
	
	protected $db, $host, $user, $password, $dbName, $port; 
	public $errno, $error, $insertId, $affectedRows, $numRows;
	
	public function __construct($dbcPar) {
		$this->host = $dbcPar['host'];
		$this->user = $dbcPar['user'];
		$this->password = $dbcPar['password'];
		$this->dbName = $dbcPar['dbName'];
		$this->port = intval($dbcPar['port']);
		$this->insertId = 0;
		$this->affectedRows = 0;
		$this->numRows = 0;
		$this->errno = 0;
		$this->error = '';
		$this->connect();
	}	
	
	public function connect() 	{
		if ($this->db) { return true; }
		$this->db = @new mysqli($this->host, $this->user, $this->password, $this->dbName, $this->port);
		$this->errno = mysqli_connect_errno();
		$this->error = mysqli_connect_error();	
		if ($this->errno != 0) {
			$this->db = null;
			return false;
		}
		return true;
	}
	
	public function close() 	{
		if (!$this->db) { return false; }
		$this->db->close();
		$this->db = NULL;
		return true;
	}
	
 	function query($sql, $aOpt='') {
 		if (!$this->connect()) return false;
		$query = $this->db->query($sql);
		$this->errno = $this->db->errno;
		$this->error = $this->db->error;	
		$this->affectedRows = $this->db->affected_rows;
		$this->numRows = (is_object($query))?$query->num_rows:0;
		$this->insertId = $this->db->insert_id;
		if ($this->errno != 0) return false;
 		return $query;
 	}
	
 	function fetch($query, $pos='') {
 		if ($pos !== '') {
 			if(!$query->data_seek(intval($pos))) {
 				return false;
 			}	
 		}	
 		$row = $query->fetch_assoc();	
 		return $row;
 	}
	
 	function free($query) {
 		$query->free();	
 		return true;
 	}
	
 	function insert($entita, $aAttrib, $aKey, $alias, $aOpt='') {
 		$this->connect();
 		$sql = "INSERT INTO ".$entita." SET ";
 		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			$sql .= substr($nomeAttr,2)." = '".addslashes($valAttr)."', "; 
 		}
 		$sql = substr($sql,0,-2);
		$query = $this->db->query($sql);
		$this->errno = $this->db->errno;
		$this->error = $this->db->error;	
		$this->insertId = $this->db->insert_id;	
		if ($this->errno != 0) return false;
		$this->free($query);
		return true;
 	}
	
 	function update($entita, $aAttrib, $aKey, $alias='', $aOpt='') {
 		$this->connect();
 		$sql = "UPDATE ".$entita." SET ";
 		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			if (in_array($nomeAttr,$aKey)) { continue; }
 			$sql .= substr($nomeAttr,2)." = '".addslashes($valAttr)."', "; 
 		}
 		$sql = substr($sql,0,-2)." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
		$query = $this->db->query($sql);
		$this->errno = $this->db->errno;
		$this->error = $this->db->error;	
		if ($this->errno != 0) return false;
		$this->affectedRows = $this->db->affected_rows;
		$this->free($query);
		return true;
 	}
 	
 	function delete($entita, $aAttrib, $aKey, $alias='', $aOpt='') {
 		$this->connect();
 		$sql = "DELETE FROM ".$entita." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
		$query = $this->db->query($sql);
		$this->errno = $this->db->errno;
		$this->error = $this->db->error;	
		if ($this->errno != 0) return false;
		$this->affectedRows = $this->db->affected_rows;
		$this->free($query);
		return true;
 	}
 	
 	function read($entita, &$aAttrib, $aKey, $alias='', $aOpt='') {
 		// $aOpt['lock'] = 'yes' : esegue select with lock
 		// $aOpt['locktype'] = "upd" : lock type per Update 		
 		// $aOpt['locktype'] = "shr" : lock type tipo shared
 		$this->connect();
 		$sql = "SELECT ";
		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			//if (in_array($nomeAttr,$aKey)) { continue; }
 			$sql .= substr($nomeAttr,2).", "; 
 		}
 		
		$sql = substr($sql,0,-2)." FROM ".$entita." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
 		if(isset($aOpt['lock']) && $aOpt['lock'] == "yes"){
 			if(isset($aOpt['locktype']) && $aOpt['locktype'] == "upd")
 				$sql .= " FOR UPDATE";
 			if(isset($aOpt['locktype']) && $aOpt['locktype'] == "shr")
 				$sql .= " LOCK IN SHARE MODE";
 		}
 		//echo("<br>SQL: $sql<br><br>");
		$query = $this->db->query($sql);
		$row = $this->fetch($query);
		if($row){
			foreach($aAttrib as $nomeAttr => $valAttr) {
	 			if ($nomeAttr[0] != $alias) { continue; }
	 			if (in_array($nomeAttr,$aKey)) { continue; }
	 			$aAttrib[$nomeAttr] = $row[substr($nomeAttr,2)]; 
	 		}
		}
			
		$this->errno = $this->db->errno;
		$this->error = $this->db->error;	
		if ($this->errno != 0) return false;
		$this->affectedRows = $this->db->affected_rows;
		$this->free($query);
		return true;
 	}
 	
 	function getFields($entita){
 		$sSQL = "SHOW COLUMNS FROM $entita";
 		$aFields = array();
 		if($query = $this->query($sSQL)){
 			while($row = $this->fetch($query)){
 				$aFields[$row["Field"]] = array("Type"=>trim($row["Type"])); 
 			}
 			return $aFields;
 		}else{
 			return false;
 		}
 	}
 	
 	function startTransaction() {
 		if($this->db->autocommit(FALSE)){
 			return true;
 		}else{ 				
	 		$this->errno = $this->db->errno;
			$this->error = $this->db->error;
			return false;	
 		}
 	}
 	
 	function commit() {
 		if($this->db->commit()){
 			return true;
 		}else{ 				
	 		$this->errno = $this->db->errno;
			$this->error = $this->db->error;
			return false;	
 		}
 	}
 	
 	function rollback() {
 		if($this->db->rollback()){
 			return true;
 		}else{ 				
	 		$this->errno = $this->db->errno;
			$this->error = $this->db->error;
			return false;	
 		}
 	}
}
?>
